﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEditor;

public static class AssetTrash
{
    static string dir = "Assets/Trash";
    static string configName = "config.txt";
    static Dictionary<string, string> nameMap;

    static AssetTrash()
    {
        if (!CommonUtil.Exists(dir))
        {
            CommonUtil.CreateDir(dir);
            AssetDatabase.Refresh();
        }

        nameMap = CommonUtil.ObjectFromJsonFile<Dictionary<string, string>>(dir + "/" + configName);
        if (nameMap != null)
        {
            HashSet<string> deleteKeySet = new HashSet<string>();
            foreach (var entry in nameMap)
            {
                if (!CommonUtil.Exists(entry.Key))
                {
                    deleteKeySet.Add(entry.Key);
                }
            }

            foreach (var deleteKey in deleteKeySet)
            {
                nameMap.Remove(deleteKey);
            }
        }
    }



    static void AddNameMap(string trashAssetPath, string assetPath)
    {
        if (nameMap == null)
        {
            nameMap = new Dictionary<string, string>();
        }

        nameMap.Add(trashAssetPath, assetPath);
    }

    static string GetTrashAssetPath(string path)
    {
        string trashAssetPath = null;
        string fileName = Path.GetFileName(path);
        if (nameMap == null || !nameMap.ContainsKey(dir + "/" + fileName))
        {
            trashAssetPath = dir + "/" + fileName;
        }
        else
        {
            while (true)
            {
                int value = Random.Range(int.MinValue, int.MaxValue);
                string trashAssetFileName = CommonUtil.addSuffixToFileName(fileName, "_" + value);
                if (!nameMap.ContainsKey(trashAssetFileName))
                {
                    trashAssetPath = dir + "/" + trashAssetFileName;
                    break;
                }
            }
        }

        return trashAssetPath;
    }

    static void WriteConfigToFile()
    {
        if (nameMap == null)
        {
            return;
        }

        CommonUtil.ObjectToJsonFile<Dictionary<string, string>>(nameMap, dir + "/" + configName);
    }

    public static void Delete(string path)
    {
        if (!CommonUtil.Exists(path))
        {
            return;
        }

        string trashAssetPath = GetTrashAssetPath(path);
        AddNameMap(trashAssetPath, path);
        WriteConfigToFile();

        AssetDatabase.MoveAsset(path, trashAssetPath);
    }

    public static void Delete(HashSet<string> paths)
    {
        Dictionary<string, string> map = new Dictionary<string, string>();
        foreach (var path in paths)
        {
            string trashAssetPath = GetTrashAssetPath(path);
            AddNameMap(trashAssetPath, path);
            map.Add(path, trashAssetPath);
        }
        WriteConfigToFile();

        foreach (var entry in map)
        {
            AssetDatabase.MoveAsset(entry.Key, entry.Value);
        }
    }

    public static void PutBackAll()
    {
        if (nameMap == null)
        {
            return;
        }


        List<string> keys = new List<string>();
        foreach (var entry in nameMap)
        {
            keys.Add(entry.Key);
        }

        for(int i = keys.Count - 1; i >= 0; i--)
        {
            string key = keys[i];
            string value = nameMap[key];
            string log = AssetDatabase.MoveAsset(key, value);
            if (!string.IsNullOrEmpty(log))
            {
                throw new System.Exception(log + ": " + key + "   |  " + value);
            }
        }
        nameMap.Clear();

        WriteConfigToFile();
    } 
}
